package com.yueya.business.assess.model;

import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.common.util.DateUtil;

import net.sf.json.JSONObject;

/**
 * 准入评估
 * @author todds
 *
 */
@SuppressWarnings("serial")
public class Assess extends Model<Assess> {
	
	public static  Assess dao = new Assess();
	
	public boolean save(Assess dao) {
		dao.set("create_time",DateUtil.getDate(DateUtil.timeFormat));
		return dao.save();
	}

	 
    public boolean update(Assess dao) {
    	dao.set("update_time",DateUtil.getDate(DateUtil.timeFormat));
		return dao.update();
	}
    
    public Assess findByMap(Map<String,Object> queryMap) {
    	String sql ="select * from jg_assess_base   where 1=1" ;
    	if(queryMap!=null) {
    		if(queryMap.get("accessid")!=null) {
    			sql+=" and access_id='"+queryMap.get("accessid").toString()+"'";
    		}
    	}
    	return dao.findFirst(sql);
    }
    
    
  public Record getAssessSeniorBaseDetail(String id) {
    	String sql = "select sb.senior_name seniorname , sb.gender ,sb.id_card idcard , sb.address , sb.birth_date birthdate,ab.access_type  ";
    	sql+="	from  jg_assess_base ab LEFT JOIN   jg_senior_base sb on ab.senior_id=sb.senior_id  ";
		sql+=" where ab.del_flag='0' and ab.access_id=?  " ;
		Record r = Db.findFirst(sql,id);
    	return r;
    }
  
  public Record getAssessSeniorHealthDetail(String id) {
  	String sql = " select sh.presentillness_history presentillnesshistory,sh.beforeillness_history beforeillnesshistory,sh.medical_history medicalhistory,sh.self_medical selfmedical,sh.temperature,sh.sphygmus,sh.breath,sh.bloodpressure,sh.heartrate  ";
  	sql+=" ,sh.height,sh.weight,sh.hearing,sh.tooth,sh.vision,sh.contagion,sh.operaction_history operactionhistory,sh.allergy_history allergyhistory,sh.family_history familyhistory,sh.smoke_history smokehistory,sh.smoke_old,sh.smoke_num,sh.drink_history,sh.drink_old ";
  	sql+=" ,sh.drink_amount drinkamount,sh.taste,sh.taste_txt tastetxt,sh.cognize,sh.cognize_txt cognizetxt,sh.emotion,sh.emotion_txt emotiontxt ";
  	sql+="	from  jg_assess_base ab LEFT JOIN   jg_senior_health sh on ab.access_id = sh.access_id  ";
	sql+=" where ab.del_flag='0' and ab.access_id=?  " ;
	Record r = Db.findFirst(sql,id);
  	return r;
  }
    
  public Record getAssessSeniorIllnessDetail(String id) {
	  	String sql = " select si.admission_date admissiondate,si.discharge_date dischargedate,si.illness_date illnessdate,si.illness_relator illnessrelator,si.relator,si.reliability,si.presentillness_history presentillnesshistory,si.beforeillness_history beforeillnesshistory  ";
	  	sql+=" ,si.allergy_history allergyhistory,si.personage_history personagehistory,si.married_history marriedhistory,si.family_history familyhistory ";
	  	sql+="	from  jg_assess_base ab LEFT JOIN   jg_senior_illness si on ab.access_id = si.access_id  ";
		sql+=" where ab.del_flag='0' and ab.access_id=?  " ;
		Record r = Db.findFirst(sql,id);
	  	return r;
  }
  
  public Record getAssessContentDetail(String id) {
	  	String sql = " select ac.access_eat accesseat,ac.access_wash accesswash,ac.access_wear accesswear,ac.access_bath accessbath,ac.access_excrete_shit accessexcreteshit,ac.access_excrete_diss accessexcretediss,ac.access_make_stool accessmakestool,ac.access_stair accessstair,ac.access_move accessmove,ac.access_activity accessactivity  ";
	  	sql+="	from  jg_assess_base ab LEFT JOIN   jg_assess_content ac on ab.access_id = ac.access_id  ";
		sql+=" where ab.del_flag='0' and ab.access_id=?  " ;
		Record r = Db.findFirst(sql,id);
	  	return r;
  }
  
  
    
    
	public Page<Record> paginate (int pageNumber,int pageSize,JSONObject jo) {
      
    	String select = "select se.*,sb.name,sb.id_card ";
    	StringBuffer sql = new StringBuffer(" from jg_assess_base se , jg_senior_base sb where sb.del_flag='0' and se.senior_id = sb.senior_id ");
		// 名字
		if (jo.containsKey("name")) {
			String name = jo.getString("name");
			if (StrKit.notBlank(name)) {
				sql.append(" and (sb.name like '%" + name + "%' or sb.id_card like  '%"+name+"%'  )");
			}
		}
		sql.append(" order by se.create_time desc");
		return Db.paginate(pageNumber, pageSize, select, sql.toString());
    }
	
    
    
}
